<?php
/** Tiehui.Lu 512701323@qq.com utf-8 下午02:02:48 2009-7-7 */

function getmicrotime() {
	list ( $usec, $sec ) = explode ( " ", microtime () );
	return (( float ) $usec + ( float ) $sec);
}

function getUsec() {
	$time = explode ( " ", microtime () );
	return $time [0];
}

function req($key) {
	if (isset ( $_REQUEST [$key] ) && is_array ( $_REQUEST [$key] )) {
		for($i = 0; $i < count ( $_REQUEST [$key] ); $i ++) {
			$ret [$i] = htmlspecialchars ( trim ( $_REQUEST [$key] [$i] ), ENT_QUOTES );
		}
	} else if (isset ( $_REQUEST [$key] )) {
		$ret = htmlspecialchars ( trim ( $_REQUEST [$key] ), ENT_QUOTES );
	} else {
		$ret = null;
	}
	return $ret;
}

function reqArr() {
	$ret = array ();
	foreach ( $_REQUEST as $key => $value ) {
		if (isset ( $_REQUEST [$key] ) && is_array ( $_REQUEST [$key] )) {
			for($i = 0; $i < count ( $_REQUEST [$key] ); $i ++) {
				$tmp [$i] = htmlspecialchars ( trim ( $_REQUEST [$key] [$i] ), ENT_QUOTES );
			}
			$ret [$key] = $tmp;
		} else if (isset ( $_REQUEST [$key] ) && ! is_array ( $_REQUEST [$key] )) {
			$ret [$key] = htmlspecialchars ( trim ( $value ), ENT_QUOTES );
		} else {
			$ret [$key] = null;
		}
	}
	return $ret;
}

function req2html($value, $stripslashes = false) {
	$value = is_array ( $value ) ? array_map ( 'req2html', $value ) : htmlspecialchars_decode ( trim ( $value ), ENT_QUOTES );
	if ($stripslashes) {
		$value = stripslashes_deep ( $value );
	}
	return $value;
}

function stripslashes_deep($value) {
	$value = is_array ( $value ) ? array_map ( 'stripslashes_deep', $value ) : stripslashes ( $value );
	return $value;
}

function rmdir2($dirname) {
	if (! is_dir ( $dirname )) {
		return false;
	}
	$handle = @opendir ( $dirname );
	while ( ($file = @readdir ( $handle )) !== false ) {
		if ($file != '.' && $file != '..') {
			$dir = $dirname . '/' . $file;
			is_dir ( $dir ) ? rmdir2 ( $dir ) : @unlink ( $dir );
		}
	}
	closedir ( $handle );
	return rmdir ( $dirname );
}

//时间比较函数，返回两个日期相差几秒、几分钟、几小时或几天$end - $start > 0
function DateDiff($start, $end, $unit = "") {
	switch ($unit) {
		case 's' :
			$dividend = 1;
			break;
		case 'i' :
			$dividend = 60;
			break;
		case 'h' :
			$dividend = 3600;
			break;
		case 'd' :
			$dividend = 86400;
			break;
		default :
			$dividend = 86400;
	}
	$start = strtotime ( $start );
	$end = strtotime ( $end );
	if ($start && $end) {
		return ( float ) ($end - $start) / $dividend;
	}
	return false;
}
function echoXml($xml) {
	$xml = "<root>\r\n$xml</root>\r\n";
	$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" . $xml;
	header ( "Content-Type: text/xml" );
	echo $xml;
}

/**
 * output random date 
 *
 * @param number $num
 * @param string $w [number],[upper],[lower],[numberupper],[numberlower],[upperlower],[numberupperlower]
 * @return output random date
 */
function getValidateNum($num = 6, $w = 'number') {
	$valcode = "";
	srand ( microtime () * 100000 );
	$number = array ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );
	$upper = array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' );
	$lower = array ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' );
	if ($w == "number") {
		for($i = 0; $i < $num; $i ++) {
			$rand = rand ( 0, count ( $number ) - 1 );
			$valcode = $valcode . $number [$rand];
		}
	} else if ($w == "upper") {
		for($i = 0; $i < $num; $i ++) {
			$rand = rand ( 0, count ( $upper ) - 1 );
			$valcode = $valcode . $upper [$rand];
		}
	} else if ($w == "lower") {
		for($i = 0; $i < $num; $i ++) {
			$rand = rand ( 0, count ( $lower ) - 1 );
			$valcode = $valcode . $lower [$rand];
		}
	} else if ($w == "numberupper") {
		$arr = array_merge ( $number, $upper );
		for($i = 0; $i < $num; $i ++) {
			$rand = rand ( 0, count ( $arr ) - 1 );
			$valcode = $valcode . $arr [$rand];
		}
	} else if ($w == "numberlower") {
		$arr = array_merge ( $number, $lower );
		for($i = 0; $i < $num; $i ++) {
			$rand = rand ( 0, count ( $arr ) - 1 );
			$valcode = $valcode . $arr [$rand];
		}
	} else if ($w == "upperlower") {
		$arr = array_merge ( $upper, $lower );
		for($i = 0; $i < $num; $i ++) {
			$rand = rand ( 0, count ( $arr ) - 1 );
			$valcode = $valcode . $arr [$rand];
		}
	} else if ($w == "numberupperlower") {
		$arr = array_merge ( $number, $upper, $lower );
		for($i = 0; $i < $num; $i ++) {
			$rand = rand ( 0, count ( $arr ) - 1 );
			$valcode = $valcode . $arr [$rand];
		}
	}
	return $valcode;
}

function highLight($key, $str) {
	$key = trim ( $key );
	$key = str_replace ( "/", " ", $key ); // Bug
	//$key = $key.replace(/\s+/g," ");//把中间的多个空格用一个空格替换(eg. "a b  cb " -> "a b cb");
	$keyArr = split ( " ", $key );
	$keyArr = grep ( $keyArr );
	for($i = 0; $i < count ( $keyArr ); $i ++) {
		$str = preg_replace ( "/$keyArr[$i]/i", "<b>" . $keyArr [$i] . "</b>", $str );
	}
	return $str;
}

/**
 * var arr = ["bd","b","abc","ab","abcd"];
 * var newArr = grep(arr)); newArr = ["abcd","bd"]
 *
 * @return Array()
 */
function grep($arr) {
	//if (strlen ( $arr ) == 1)
	if (count ( $arr ) == 1)
		return $arr;
		
	// sort, get array link ["abcd","abc","bd","ab","a"]
	usort ( $arr, "cmpLength" );
	// if contain, set value ""
	for($i = 0; $i < count ( $arr ) - 1; $i ++) {
		for($j = $i + 1; $j < count ( $arr ); $j ++) {
			if ($arr [$j] == "")
				break;
			if (strstr ( $arr [$i], $arr [$j] ) != "") {
				$arr [$j] = "";
			}
		}
	}
	// Remove the previous set "" Item
	$newArr = array ();
	$count = 0;
	for($i = 0; $i < count ( $arr ); $i ++) {
		if ($arr [$i] != "") {
			$newArr [$count] = $arr [$i];
			$count ++;
		}
	}
	return $newArr;
}

function cmpLength($a, $b) {
	return strlen ( $b ) - strlen ( $a );
}
?>